_package("alz.core");

/**
 * Action管理者类(Pane专用元素管理类)
 */
_class("ActionManager", "", function(){
	/**
	 * @param {AObject} actionEngine 实现了doAction方法的action响应类
	 */
	this._init = function(actionEngine){
		_super._init.call(this);
		this._actionEngine = null;  //动作执行引擎，实现了doAction接口的类的实例
		//this._uid = 0;
		this._hash = {};
		this._list = [];
		this._actionEngine = actionEngine;
	};
	this.dispose = function(){
		for(var i = 0, len = this._list.length; i < len; i++){
			this._list[i] = null;
		}
		this._list = [];
		for(var k in this._hash){
			//runtime._element.remove(this._hash[k]);
			//this._hash[k].removeAttribute("aui_aid");
			this._hash[k] = null;
			delete this._hash[k];
		}
		this._actionEngine = null;
		_super.dispose.apply(this);
	};
	/**
	 * 获取action响应类
	 */
	this.getActionEngine = function(){
		return this._actionEngine;
	};
	/**
	 * 添加一个带_action属性的DOM元素
	 * @param {Element} node DOM元素
	 */
	this.add = function(node){
		var act = runtime._element.data(node, "action");
		if(!act){
			act = node.getAttribute("_action");
			runtime._element.data(node, "action", act);
		}
		var uid = runtime._element.getUid(node);
		//var uid = this._uid++;
		//node.setAttribute("aui_aid", uid);
		this._hash[uid] = node;
		this._list.push(node);
	};
	/**
	 * 遍历所有元素并执行指定的函数
	 * @param {Function} func 要执行的函数
	 */
	this.each = function(func){
		for(var i = 0, len = this._list.length; i < len; i++){
			func(i, this._list[i]);
		}
	};
	/**
	 * 分派一个action
	 * @param {String} name action的名字
	 * @param {Element} sender action发送者
	 * @param {Event} ev 原始的事件
	 */
	this.dispatchAction = function(name, sender, ev){
		if(this._actionEngine.doAction){
			return this._actionEngine.doAction(name, sender, ev);
		}
	};
});